/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Apps4Italy PM10 project.
 * 
 * PM10 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * PM10 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.data;

import it.rainbowbreeze.libs.data.RainbowAppPreferencesDao;
import it.rainbowbreeze.libs.helper.RainbowArrayHelper;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Main application preferences data storage
 * 
 * @author Alfredo "Rainbowbreeze" Morresi
 */
public class AppPreferencesDao
    extends RainbowAppPreferencesDao
{
    // ------------------------------------------ Private Fields
    
    private static final String PROP_LAST_LATITUDE = "last_longitude";
    private static final String PROP_LAST_LONGITUDE = "last_latitude";
    private static final String PROP_LAST_ZOOM = "last_zoom";
    private static final String PROP_CATEGORY_IDS = "category_ids";
    private static final String PROP_MEASURE_IDS = "measure_ids";
    
    private static final String PROP_SEPARATOR = ",";
    

    // -------------------------------------------- Constructors
    public AppPreferencesDao(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }


    // --------------------------------------- Public Properties

    
    // ------------------------------------------ Public Methods
    public int getLastLatitude()
    { return mSettings.getInt(PROP_LAST_LATITUDE, 42000000); }
    public void setLastLatitude(int newValue)
    { mEditor.putInt(PROP_LAST_LATITUDE, newValue); }

    public int getLastLongitude()
    { return mSettings.getInt(PROP_LAST_LONGITUDE, 12000000); }
    public void setLastLongitude(int newValue)
    { mEditor.putInt(PROP_LAST_LONGITUDE, newValue); }

    public int getLastZoom()
    { return mSettings.getInt(PROP_LAST_ZOOM, 7); }
    public void setLastZoom(int newValue)
    { mEditor.putInt(PROP_LAST_ZOOM, newValue); }

    public int[] getCategoryIds()
    { return getIntArray(PROP_CATEGORY_IDS); }
    public void setCategoryIds(int[] newValue)
    { setIntArray(PROP_CATEGORY_IDS, newValue); }

    public int[] getMeasureIds()
    { return getIntArray(PROP_MEASURE_IDS); }
    public void setMeasureIds(int[] newValue)
    { setIntArray(PROP_MEASURE_IDS, newValue); }


    // ----------------------------------------- Private Methods
    /* (non-Javadoc)
     * @see com.angurialab.postino.data.BasePreferencesDao#backupProperties()
     */
    @Override
    protected void backupProperties(SharedPreferences.Editor editorBackup) {
    }

    /* (non-Javadoc)
     * @see com.angurialab.postino.data.BasePreferencesDao#restoreProperties()
     */
    @Override
    protected void restoreProperties(SharedPreferences settingsBackup) {
    }
    
    private int[] getIntArray(String propKey) {
        String ids = mSettings.getString(propKey, null);
        return RainbowArrayHelper.splitToIntArray(ids, PROP_SEPARATOR);
    }

    private void setIntArray(String propKey, int[] data) {
        String ids = RainbowArrayHelper.join(data, PROP_SEPARATOR);
        mEditor.putString(propKey, ids);
    }
    
    
}